#pragma once

#include <stdint.h>
#include <boost/asio.hpp>
#include <boost/bind.hpp>

using boost::asio::ip::tcp;

template < class Handler, uint16_t Port, class Init > class Client
{
	public:
		Client( boost::asio::io_service &io, tcp::resolver::iterator end_it, Init init ) : io_s( io )
		{
			handler = init( io );

			//tcp::socket &s = handler->getSocket();

			handler->getSocket()->connect( *end_it );

			//handler->getSocket().async_connect( *end_it, boost::bind( &Client::handle_connect, this, boost::asio::placeholders::error, ++end_it ) );
		}
		
		~Client(void)
		{
			handler->getSocket()->close();
			delete handler;
		}

		void handle_connect( const boost::system::error_code &error, tcp::resolver::iterator end_it )
		{
			/*if( !error )
			{
				boost::asio::async_read( handler->getSocket(), 
					boost::asio::buffer( handler->getBuf(), handler->getBufLength() ), 
					boost::bind( &Handler::handle_read, handler, boost::asio::placeholders::error ) );
			}
			else if( end_it != tcp::resolver::iterator() )
			{
				handler->getSocket().async_connect( *end_it, boost::bind( &Client::handle_connect, this, boost::asio::placeholders::error, ++end_it ) );
			}*/
		}

	private:
		boost::asio::io_service &io_s;
		Handler *handler;
};

